package org.gpp.proj1.ui.gui.editor;

import java.util.List;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import org.gpp.proj1.logic.metadata.RobotEntry;
import org.gpp.proj1.ui.gui.SRGGUIView;
import org.gpp.proj1.ui.gui.command.SRGGUICommandFactory;
import org.gpp.proj1.ui.gui.jme.RobotDisplayPanel;

public class RobotListEditorWindow extends JFrame implements ListSelectionListener {

	private static final long serialVersionUID = -8363409953417262863L;

	private JButton editRobotButton;
	private JButton deleteRobotButton;
	private JButton newRobotButton;
	private JLabel robotsLabel;
	private JLabel robotNameLabel;
	private JList robotList;
	private JPanel robotPanel;
	private JScrollPane scrollPane;
	private JScrollPane robotDataPane;
	private JTextArea robotData;
	private RobotDisplayPanel displayPanel;

	private SRGGUIView view;

	private List<RobotEntry> robots;

	private String selectedRobot;

	private int selectedIndex = -1;

	public RobotListEditorWindow(SRGGUIView view) {

		this.view = view;
		this.initComponents();

		this.setLocationRelativeTo( null );
	}

	private void initComponents() {
		scrollPane = new javax.swing.JScrollPane();
		robotList = new javax.swing.JList();
		editRobotButton = new javax.swing.JButton();
		deleteRobotButton = new javax.swing.JButton();
		newRobotButton = new javax.swing.JButton();
		robotsLabel = new javax.swing.JLabel();
		robotNameLabel = new javax.swing.JLabel();
		robotDataPane = new javax.swing.JScrollPane();
		robotData = new javax.swing.JTextArea();
		robotPanel = new javax.swing.JPanel();

		setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
		setResizable(false);
		robotList.setModel(new javax.swing.AbstractListModel() {
			String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
			public int getSize() { return strings.length; }
			public Object getElementAt(int i) { return strings[i]; }
		});
		scrollPane.setViewportView(robotList);
		
		displayPanel = new RobotDisplayPanel( view, 316, 334 );

		editRobotButton.setText("Edit");
		editRobotButton.setActionCommand( SRGGUICommandFactory.OPEN_ROBOT_EDITOR_COMMAND );
		editRobotButton.addActionListener( view );

		deleteRobotButton.setText("Delete");
		deleteRobotButton.setActionCommand( SRGGUICommandFactory.DELETE_ROBOT_COMMAND );
		deleteRobotButton.addActionListener( view );

		newRobotButton.setText("New");
		newRobotButton.setActionCommand( SRGGUICommandFactory.OPEN_ROBOT_CREATOR_COMMAND );
		newRobotButton.addActionListener( view );

		robotsLabel.setText("Your robots");

		robotNameLabel.setFont(new java.awt.Font("Dialog", 1, 18));
		robotNameLabel.setText("Robot Name");

		robotData.setText("No robot is selected");
		robotData.setColumns(20);
		robotData.setRows(5);
		robotData.setBorder(null);
		robotDataPane.setViewportView(robotData);

		robotPanel.setBackground(new java.awt.Color(204, 204, 204));
		org.jdesktop.layout.GroupLayout robotPanelLayout = new org.jdesktop.layout.GroupLayout(robotPanel);
		robotPanel.setLayout(robotPanelLayout);
		robotPanelLayout.setHorizontalGroup(
				robotPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
				.add(0, 316, Short.MAX_VALUE)
		);
		robotPanelLayout.setVerticalGroup(
				robotPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
				.add(0, 331, Short.MAX_VALUE)
		);

		org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
		getContentPane().setLayout(layout);
		layout.setHorizontalGroup(
				layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
				.add(layout.createSequentialGroup()
						.addContainerGap()
						.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
								.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING, false)
										.add(org.jdesktop.layout.GroupLayout.LEADING, scrollPane)
										.add(org.jdesktop.layout.GroupLayout.LEADING, layout.createSequentialGroup()
												.add(newRobotButton)
												.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
												.add(editRobotButton)
												.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
												.add(deleteRobotButton)))
												.add(robotsLabel))
												.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
												.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
														.add(robotNameLabel)
														.add(robotPanel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
														.add(robotDataPane, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 316, Short.MAX_VALUE))
														.addContainerGap())
		);
		layout.setVerticalGroup(
				layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
				.add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
						.addContainerGap()
						.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
								.add(robotsLabel)
								.add(robotNameLabel))
								.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
								.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
										.add(layout.createSequentialGroup()
												.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
												.add(robotPanel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
												.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
												.add(robotDataPane, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 124, Short.MAX_VALUE))
												.add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
														.add(scrollPane, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 430, Short.MAX_VALUE)
														.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
														.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
																.add(newRobotButton)
																.add(editRobotButton)
																.add(deleteRobotButton))))
																.addContainerGap())
		);
		
		robotPanel.add( displayPanel );
		
		pack();

		this.update();
		this.robotList.addListSelectionListener( this );
		this.robotList.setSelectionMode( ListSelectionModel.SINGLE_SELECTION );
	}

	/**
	 * Updates the list. Invoked when a robot is created, updated or deleted
	 */
	public void update() {

		DefaultListModel model = new DefaultListModel();

		this.robots = view.getPlayerRobots();
		for( RobotEntry robot : robots ) {
			model.addElement( robot.getRobotName() );
		}

		this.robotList.setModel( model );

		if( model.getSize() == 0 ) {
			this.selectedIndex = -1;
			this.selectedRobot = null;
		}

		if( this.selectedIndex > 0 ) {
			if( this.selectedIndex >= model.getSize() ) {
				this.selectedIndex = model.getSize() - 1;
			}

			this.robotList.setSelectedIndex( selectedIndex );
			
			RobotEntry entry = this.view.getController().getCurrentProfile().getRobot( selectedRobot );
			this.displayPanel.setRobot( entry );
		}

		this.updateRobotdata();
		this.pack();
	}

	public String getSelectedRobot() {

		return selectedRobot;
	}

	public void valueChanged(ListSelectionEvent e) {

		if( robotList.getSelectedValue() != null ) {
			this.selectedRobot = (String) robotList.getSelectedValue();
			this.selectedIndex  = robotList.getSelectedIndex();

			if( selectedRobot != null && ! selectedRobot.trim().equals( "" ) ) {			
				this.robotNameLabel.setText( selectedRobot );			
				this.robotData.setText( this.view.getController().getRobotDescription( selectedRobot ) );
				
				RobotEntry entry = this.view.getController().getCurrentProfile().getRobot( selectedRobot );
				this.displayPanel.setRobot( entry );
			}
		}
	}

	private void updateRobotdata() {

		if( this.selectedRobot != null &&
				view.getController().getCurrentProfile().existsRobot( this.selectedRobot ) ) {

			this.robotNameLabel.setText( selectedRobot );			
			this.robotData.setText( this.view.getController().getRobotDescription( selectedRobot ) );
		} else {
			this.robotNameLabel.setText( "Select a robot" );	
			this.robotData.setText("No robot is selected");
		}
	}

}
